package com.egghiro.j2se.game.sace.model.actor.bullet;

import com.egghiro.framework.j2se.game.core.model.actor.IActor2D;
import com.egghiro.framework.j2se.game.core.view.SImage;
import com.egghiro.framework.j2se.game.model.element.LinearVelocity2D;
import com.egghiro.framework.j2se.game.utils.PropertyUtils;
import com.egghiro.j2se.game.sace.model.actor.Flyer;
import com.egghiro.j2se.game.sace.param.SACEConst;
import com.egghiro.j2se.game.sace.state.actorstate.FlyerForwardState;
import com.egghiro.j2se.game.sace.state.actorstate.FlyerStandByState;

public class AimBullet extends Bullet {
    private static final double DEFAULT_SPEED = 5;
    // private static final SImage IMG = SImage.createImage(10, 10);
    // static {
    // IMG.getSGraphics().setColor(Color.GREEN);
    // IMG.getSGraphics().fillRect(0, 0, IMG.getWidth(), IMG.getHeight());
    // }
    private static final SImage IMG = SImage.createImage(PropertyUtils.getAIMPropertyValue(SACEConst.AIM_P_DIR,
            SACEConst.AIM_P_Key_IMG_REIMU_FU));

    public AimBullet(int x, int y, double angle, IActor2D target, long life) {
        this(x, y, DEFAULT_SPEED, angle, target, life);
    }

    public AimBullet(int x, int y, double speed, double angle, IActor2D target, long life) {
        this(x, y, new LinearVelocity2D(speed, angle), target, life);
    }

    public AimBullet(int x, int y, LinearVelocity2D speed, IActor2D target, long life) {
        super(x, y, AimBullet.IMG, target, life);
        setState(new ForwardStateBeforeAim(this, speed));
    }

    private class ForwardStateBeforeAim extends FlyerForwardState {
        private long delay = 300;

        public ForwardStateBeforeAim(Flyer flyer, LinearVelocity2D speed) {
            super(flyer, speed);
        }

        @Override
        public void countDown() {
            if ((delay -= SACEConst.SLEEP) <= 0) {
                getFlyer().setState(new StandByAndAimState(getFlyer(), speed));
            }
        }
    }

    private class StandByAndAimState extends FlyerStandByState {
        private long delay = 500;

        public StandByAndAimState(Flyer flyer, LinearVelocity2D speed) {
            super(flyer, speed);
        }

        @Override
        public void countDown() {
            if ((delay -= SACEConst.SLEEP) <= 0) {
                getFlyer().setState(new FlyerForwardState(getFlyer(), speed));
            }

        }

    }
}
